package wechat.service.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import wechat.bean.request.RequestResultBean;
import wechat.resources.InstanceResource;
import wechat.util.RespCode;
import wechat.util.http.HttpClientUtil;

public class BaseRequest {
	private Logger log = LoggerFactory.getLogger(BaseRequest.class);

	protected int Deadline = 7200;

	protected String fetchAcessToken(InstanceResource instanceResource) throws Exception {
		String url = WechatApi.getApi_access_token(instanceResource);
		String result = HttpClientUtil.get(url);
		JSONObject jsonResult = JSONObject.parseObject(result);

		checkResult(jsonResult);

		System.out.println("access_token :" + jsonResult.getString("access_token"));
		System.out.println("expires_in :" + jsonResult.get("expires_in"));
		log.info("BaseRequest--fetchAcessToken access_token=" + jsonResult.getString("access_token") + " expires_in="+ jsonResult.get("expires_in"));

		String access_token = jsonResult.getString("access_token");

		return access_token;
	}

	protected String fetchJSTicket(String access_token) throws Exception {
		String ticket = null;

		String url = WechatApi.getJsticket(access_token);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		checkResult(jsonResult);

		log.info("BaseRequest--fetchJSTicket ticket=" + jsonResult.getString("ticket") + " expires_in=" + jsonResult.get("expires_in"));

		ticket = jsonResult.getString("ticket");

		return ticket;

	}
	
	protected String fetchUserId(String access_token,String code) throws Exception {
		String userId = null;

		String url = WechatApi.getUser_token(access_token, code);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		checkResult(jsonResult);

		log.info("BaseRequest--fetchUserId UserId=" + jsonResult.getString("UserId") + " DeviceId=" + jsonResult.get("DeviceId"));

		userId = jsonResult.getString("UserId");

		return userId;
	}
	
	protected String convertToOpenid(String access_token,String param) throws Exception {
		String openid = null;

		String url = WechatApi.getConvert_to_openid(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		checkResult(jsonResult);

		log.info("BaseRequest--convertToOpenid  openid=" + jsonResult.getString("openid") + " appid=" + jsonResult.get("appid"));

		openid = jsonResult.getString("openid");

		return openid;
	}
	
	protected String convertToUserid(String access_token,String param) throws Exception {
		String userId = null;

		String url = WechatApi.getConvert_to_userid(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		checkResult(jsonResult);

		log.info("BaseRequest--convertToUserid  userid=" + jsonResult.getString("userid"));

		userId = jsonResult.getString("userid");

		return userId;
	}
	
	protected String sendMessage(String access_token,String param) throws Exception {
		return sendMessage(access_token, param, true);
	}
	
	protected String sendMessage(String access_token,String param,boolean isCheck) throws Exception {
		String url = WechatApi.getSend_message(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--sendMessage  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	
	protected String createUser(String access_token,String param,boolean isCheck) throws Exception {
		String url = WechatApi.getCreate_user(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--createUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	protected String updateUser(String access_token,String param,boolean isCheck) throws Exception {
		String url = WechatApi.getUpdate_user(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--updateUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	protected String deleteUser(String access_token,String userid,boolean isCheck) throws Exception {
		String url = WechatApi.getDelete_user(access_token, userid);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--deleteUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	protected String batchdeleteUser(String access_token,String param,boolean isCheck) throws Exception {
		String url = WechatApi.getBatchdelete_user(access_token);
		String result = HttpClientUtil.post(url, param);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--batchdeleteUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	protected String getUser(String access_token,String userid,boolean isCheck) throws Exception {
		String url = WechatApi.getGet_user(access_token, userid);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--getUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	
	protected String simplelistUser(String access_token,String department_id,String fetch_child,String status,boolean isCheck) throws Exception {
		String url = WechatApi.getSimplelist_user(access_token, department_id, fetch_child, status);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--simplelistUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	
	protected String listUser(String access_token,String department_id,String fetch_child,String status,boolean isCheck) throws Exception {
		String url = WechatApi.getList_user(access_token, department_id, fetch_child, status);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--listUser  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	
	protected String listDepartment(String access_token,String department_id,boolean isCheck) throws Exception {
		String url = WechatApi.getList_department(access_token, department_id);
		String result = HttpClientUtil.get(url);

		JSONObject jsonResult = JSONObject.parseObject(result);
		if (isCheck) {
			checkResult(jsonResult);
		}
		

		log.info("BaseRequest--listDepartment  jsonResult=" + jsonResult);

		return jsonResult.toJSONString();
	}
	
	
	
	

	private void checkResult(JSONObject jsonResult) throws Exception {
		if (jsonResult == null) {
			throw new Exception("返回结果为null");
		}

		RequestResultBean resultBean = new RequestResultBean();
		log.info("BaseRequest--checkResult  jsonResult=" + jsonResult.toJSONString());

		if (jsonResult.containsKey("errcode")) {
			resultBean = JSON.parseObject(jsonResult.toJSONString(), RequestResultBean.class);

			int errcode = jsonResult.getIntValue("errcode");
			String errmsg = jsonResult.getString("errmsg");
			String msgid = jsonResult.getString("msgid");
			String errmsgdesc = RespCode.getMessage(errcode);

			// System.out.println("errcode :" + errcode);
			// System.out.println("errmsg :" + errmsg);
			// System.out.println(errmsgdesc);

			log.info("BaseRequest--checkResult  errcode=" + errcode + " errmsg=" + errmsg + " msgid=" + msgid
					+ " errmsgdesc=" + errmsgdesc);

			resultBean.setErrcode(errcode);
			resultBean.setErrmsg(errmsg);
			resultBean.setMsgid(msgid);
			resultBean.setErrmsgdesc(errmsgdesc);
		}

		if (resultBean.getErrcode() != 0) {
			throw new Exception(
					resultBean.getErrcode() + " - " + resultBean.getErrmsg() + " - " + resultBean.getErrmsgdesc());
		}
	}

}
